package com.xw.unification.entity.request;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.IOException;

/**
 * @author qiwei
 */
public abstract class RepeatableHttpServletRequest extends HttpServletRequestWrapper {

    private static final Logger LOGGER = LoggerFactory.getLogger(RepeatableHttpServletRequest.class);

    protected HttpServletRequest request;

    public RepeatableHttpServletRequest(HttpServletRequest request) {
        super(checkRequest(request));
        this.request = request;
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        return getInputStreamWrapper();
    }

    protected abstract ServletInputStreamWrapper getInputStreamWrapper() throws IOException;

    private static HttpServletRequest checkRequest(HttpServletRequest request) {
        if (request == null) {
            throw new IllegalArgumentException("HttpServletRequest can not be null");
        }
        return request;
    }

}
